<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" class="group-form-dialog">
    <div class="dialog-content">
      <!-- 头部区域 -->
      <div class="dialog-header">
        <div class="header-icon">
          <Icon icon="ep:user-filled" />
        </div>
        <div class="header-content">
          <h3 class="dialog-title">{{ dialogTitle }}</h3>
          <p class="dialog-subtitle">配置班组基本信息</p>
        </div>
      </div>

      <!-- 表单区域 -->
      <div class="form-container">
        <el-form
          ref="formRef"
          :model="formData"
          :rules="formRules"
          label-width="110px"
          v-loading="formLoading"
          class="enhanced-form"
        >
          <div class="form-grid">
            <el-form-item label="班组名称" prop="name" class="form-item">
              <div class="input-wrapper">
                <Icon icon="ep:user-filled" class="input-icon" />
                <el-input
                  v-model="formData.name"
                  placeholder="请输入班组名称"
                  class="enhanced-input"
                />
              </div>
            </el-form-item>

            <el-form-item label="呼叫分配模式" prop="callDistribution" class="form-item">
              <div class="select-wrapper">
                <Icon icon="ep:connection" class="select-icon" />
                <el-select
                  v-model="formData.callDistribution"
                  placeholder="请选择呼叫分配模式"
                  class="enhanced-select"
                >
                  <el-option
                    v-for="item in distributionList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                    <div class="option-content">
                      <Icon :icon="getDistributionIcon(item.id)" class="option-icon" />
                      <span class="option-text">{{ item.name }}</span>
                    </div>
                  </el-option>
                </el-select>
              </div>
            </el-form-item>

            <el-form-item label="班组主管" prop="ownerUserId" class="form-item">
              <div class="select-wrapper">
                <Icon icon="ep:user" class="select-icon" />
                <el-select
                  v-model="formData.ownerUserId"
                  filterable
                  placeholder="请选择班组主管"
                  class="enhanced-select"
                >
                  <el-option
                    v-for="item in userOptions"
                    :key="parseInt(item.id)"
                    :label="item.nickname"
                    :value="parseInt(item.id)"
                  >
                    <div class="option-content">
                      <Icon icon="ep:user" class="option-icon" />
                      <span class="option-text">{{ item.nickname }}</span>
                    </div>
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false" class="cancel-btn">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button @click="submitForm" type="primary" :disabled="formLoading" class="confirm-btn">
          <Icon icon="ep:check" class="btn-icon" />
          确定
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as UserApi from '@/api/system/user'
import * as GroupApi from '@/api/call/group'

const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  name: undefined,
  callDistribution: undefined,
  ownerUserId: undefined
})

// 优化到字典中
// 0-轮选；1-最少；2-随机；7-根据坐席级别；8-根据空闲时长
const distributionList = [
  {
    id: 2,
    name: '随机'
  },
  {
    id: 7,
    name: '根据坐席级别'
  },
  {
    id: 8,
    name: '根据空闲时长'
  }
]

// 获取分配模式图标
const getDistributionIcon = (id) => {
  switch (id) {
    case 2:
      return 'ep:refresh'
    case 7:
      return 'ep:star'
    case 8:
      return 'ep:timer'
    default:
      return 'ep:connection'
  }
}

const formRules = reactive({
  name: [{ required: true, message: '班组名称不能为空', trigger: 'blur' }],
  callDistribution: [{ required: true, message: '请选择呼叫分配模式', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await GroupApi.getGroup(id)
    } finally {
      formLoading.value = false
    }
  }
  userOptions.value = await UserApi.getSimpleUserList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as GroupApi.GroupVO
    if (formType.value === 'create') {
      await GroupApi.createGroup(data)
      message.success(t('common.createSuccess'))
    } else {
      await GroupApi.updateGroup(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    name: undefined,
    thirdPartyId: undefined,
    ownerUserId: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
.group-form-dialog {
  :deep(.el-dialog) {
    border-radius: 12px;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 20px auto;
    }
  }

  :deep(.el-dialog__header) {
    display: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: 1px solid #e5e7eb;
  }
}

.dialog-content {
  background: #f8fafc;
}

/* 对话框头部 */
.dialog-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  color: white;
  border-radius: 10px;

  @media (max-width: 768px) {
    padding: 20px;
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
}

.dialog-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.dialog-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 20px;
  }
}

.enhanced-form {
  .form-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;

    @media (max-width: 768px) {
      gap: 16px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
      font-size: 14px;
      margin-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 13px;
        margin-bottom: 6px;
      }
    }

    :deep(.el-form-item__content) {
      line-height: normal;
    }
  }
}

/* 输入框样式 */
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 12px;
  color: #6b7280;
  font-size: 16px;
  z-index: 1;
}

.enhanced-input {
  width: 100%;

  :deep(.el-input__wrapper) {
    padding-left: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    min-height: 40px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input__inner) {
    font-size: 14px;

    @media (max-width: 768px) {
      font-size: 16px; // 防止iOS缩放
    }
  }
}

/* 选择框样式 */
.select-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.select-icon {
  position: absolute;
  left: 12px;
  color: #6b7280;
  font-size: 16px;
  z-index: 1;
}

.enhanced-select {
  width: 100%;

  :deep(.el-input__wrapper) {
    padding-left: 40px;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    min-height: 40px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input__inner) {
    font-size: 14px;

    @media (max-width: 768px) {
      font-size: 16px; // 防止iOS缩放
    }
  }

  :deep(.el-input__suffix) {
    right: 12px;
  }
}

/* 选项内容样式 */
.option-content {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.option-icon {
  color: #6b7280;
  font-size: 14px;
  flex-shrink: 0;
}

.option-text {
  color: #374151;
  font-size: 14px;
  font-weight: 500;
}

/* 下拉面板样式 */
:deep(.el-select-dropdown) {
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 82, 204, 0.15);
  border: 1px solid rgba(0, 82, 204, 0.1);

  .el-select-dropdown__item {
    padding: 8px 12px;
    border-radius: 4px;
    margin: 2px 4px;
    transition: all 0.2s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.05);
    }

    &.selected {
      background: rgba(0, 82, 204, 0.1);
      color: #0052cc;
      font-weight: 500;

      .option-icon {
        color: #0052cc;
      }
    }
  }
}

/* 对话框底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
  }
}

.cancel-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  background: white;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.2s ease;
  min-height: 40px;

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }

  @media (max-width: 768px) {
    width: 100%;
    order: 2;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.confirm-btn {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border: none;
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.2s ease;
  min-height: 40px;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    order: 1;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(4px);
}

:deep(.el-loading-spinner) {
  .circular {
    width: 32px;
    height: 32px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .dialog-content {
    background: #1f2937;
  }

  .form-container,
  .dialog-footer {
    background: #374151;
  }

  .dialog-title {
    color: #f9fafb;
  }

  .enhanced-input,
  .enhanced-select {
    :deep(.el-input__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .input-icon,
  .select-icon {
    color: #d1d5db;
  }

  .option-text {
    color: #f9fafb;
  }

  .option-icon {
    color: #d1d5db;
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #f9fafb;
      }
    }
  }

  :deep(.el-select-dropdown) {
    background: #374151;
    border-color: rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);

    .el-select-dropdown__item {
      color: #f9fafb;

      &:hover {
        background: rgba(96, 165, 250, 0.1);
      }

      &.selected {
        background: rgba(96, 165, 250, 0.2);
        color: #60a5fa;

        .option-icon {
          color: #60a5fa;
        }
      }
    }
  }

  :deep(.el-loading-mask) {
    background: rgba(55, 65, 81, 0.9);
  }
}

/* 高对比度模式适配 */
@media (prefers-contrast: high) {
  .enhanced-input,
  .enhanced-select {
    :deep(.el-input__wrapper) {
      border: 2px solid #0052cc;
      box-shadow: none;

      &:hover,
      &.is-focus {
        border-color: #1d4ed8;
        box-shadow: 0 0 0 2px rgba(0, 82, 204, 0.2);
      }
    }
  }

  .cancel-btn {
    border: 2px solid #6b7280;
  }

  .confirm-btn {
    background: #0052cc;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }

  :deep(.el-select-dropdown) {
    border: 2px solid #0052cc;
  }
}

/* 减少动画模式适配 */
@media (prefers-reduced-motion: reduce) {
  .enhanced-input,
  .enhanced-select,
  .cancel-btn,
  .confirm-btn {
    transition: none;
  }

  .confirm-btn {
    &:hover {
      transform: none;
    }
  }

  :deep(.el-select-dropdown__item) {
    transition: none;
  }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
  .enhanced-input,
  .enhanced-select {
    :deep(.el-input__wrapper) {
      min-height: 44px; // 增加触摸目标大小
    }
  }

  .cancel-btn,
  .confirm-btn {
    min-height: 44px;
    padding: 12px 20px;
  }

  :deep(.el-select-dropdown__item) {
    min-height: 44px;
    padding: 12px;
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .dialog-header {
    padding: 16px;
  }

  .form-container {
    padding: 16px;
  }

  .dialog-footer {
    padding: 12px 16px;
  }

  .enhanced-form {
    .form-grid {
      gap: 12px;
    }
  }
}

/* 横屏模式适配 */
@media (max-width: 768px) and (orientation: landscape) {
  .dialog-header {
    padding: 16px 20px;
    flex-direction: row;
    text-align: left;
  }

  .form-container {
    padding: 16px 20px;
  }

  .enhanced-form {
    .form-grid {
      grid-template-columns: repeat(2, 1fr);
      gap: 16px;
    }
  }
}
</style>
